#!/usr/bin/env python3

import requests
import json
import http.client,urllib.request,urllib.parse,urllib.error
from user_agent import generate_user_agent
from config import *
import time
import random
FIELD_ORI_LIST_two = ['Biology','Physics','Materials science','Chemistry','Environmental science','Mathematics','Computer science','Psychology','Sociology','Geology','Political science','Geography','Medicine','History','Engineering','Philosophy','Economics','Art','Business']




class get_micro_search_data(object):
    def __init__(self,field_name):
        self.field_name = field_name

    '''
    调用micro_search接口
    '''
    def use_micro_search_interface(self):
        #定义url
        # url = 'https://academic.microsoft.com/search?q=' + self.field_name + '&f=&orderBy=0&skip=0&take=10'
        headers = {
            "Content-Type":"application/json"
        }
        url = 'https://academic.microsoft.com/api/search'
        data = json.dumps({
            'filters': [],
            'includeCitationContexts': False,
            'orderBy': 0,
            'query': self.field_name,
            'queryExpression': "",
            'skip': 0,
            'sortAscending': True,
            'take': 10
        })
        #调用micro_search
        try:
            time.sleep(random.randint(0,3))
            r = requests.post(url,data = data,headers = headers,timeout = 10)
            if r.status_code == 200:
                print("返回状态码为200")
                ori_field_list = []
                json_data = json.loads(r.content)
                json_data = json_data.get("f")
                # print(json_data[3].get("fi"))
                # print('json_data--------------------',json_data)
                if json_data:
                    if json_data[3].get("fi"):
                        for json_item in json_data[3].get("fi"):
                            # print(json_item.get("dn"))
                            # if json_item.get("dn") in FIELD_ORI_LIST:
                            if json_item.get("dn") in FIELD_ORI_LIST_two:
                                ori_field_list.append(json_item.get("dn"))
                        time.sleep(random.randint(0,5))
                        return ori_field_list,200
                    else:
                        return [],r.status_code
                else:
                    return [],r.status_code
            else:
                return [],r.status_code
        except:
            print("出错-------------------")
            return [],403
